package com.sskj.pay.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.PayTypeEnum;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.pay.R;
import com.sskj.pay.R2;
import com.sskj.pay.bean.PayTypeBean;
import com.sskj.pay.component.DaggerUserDataComponent;
import com.sskj.pay.presenter.PayTypeActivityPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


@Route(path = RConfig.PAY_PAY_TYPE)//支付方式
public class PayTypeActivity extends BaseActivity<PayTypeActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    private SlimAdapter slimAdapter;
    @Inject
    UserViewModel
    userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.pay_activity_pay_type;
    }

    @Override
    public PayTypeActivityPresenter getPresenter() {
        return new PayTypeActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        setTitle(App.INSTANCE.getString(R.string.pay_payTypeActivity1));
        HashMap<PayTypeEnum, Integer> payTypeMap = new HashMap<>();
        payTypeMap.put(PayTypeEnum.WX, R.drawable.pay_shape_11a_corner20);
        payTypeMap.put(PayTypeEnum.ALIPAY, R.drawable.pay_shape_059_corner20);
        payTypeMap.put(PayTypeEnum.BANK, R.drawable.pay_shape_e88_corner20);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.pay_recy_item_pay_type, new SlimInjector<PayTypeBean>() {
            @Override
            public void onInject(PayTypeBean data, IViewInjector injector, List payloads) {

                injector
                        .background(R.id.clRoot, payTypeMap.get(data.getPayTypeEnum()))
                        .text(R.id.tvPayType, data.getPayTypeEnum().getName())
                        .text(R.id.tvAccount, data.getAccount())
                        .image(R.id.ivPayType, data.getPayTypeEnum().getWhiteRes())
                        .visibility(R.id.tvEdit, data.isAdd() ? View.VISIBLE : View.GONE)
                        .visibility(R.id.ivInto, data.isAdd() ? View.GONE : View.VISIBLE)
                        .visibility(R.id.llCheckbox, data.isAdd() ? View.VISIBLE : View.GONE)
                        .checked(R.id.checkbox, data.isOpen())
                        .clicked(R.id.llCheckbox, (v) -> {
                            mPresenter.togglePayType(!data.isOpen(), data.getPayTypeEnum().getType());
                        })
                        .clicked(R.id.tvEdit, v -> {
                            if (!AppCircleCheckUtil.checkTradePwd(PayTypeActivity.this,userData)){
                                return;
                            }
                            if (!AppCircleCheckUtil.checkAuth(PayTypeActivity.this,userData)){
                                return;
                            }
                            ARouter.getInstance().build(RConfig.PAY_ADD_PAY_TYPE).withSerializable(Constans.PAY_TYPE, data).navigation();

                        })
                        .clicked(R.id.ivInto, v -> {
                            if (!AppCircleCheckUtil.checkTradePwd(PayTypeActivity.this,userData)){
                                return;
                            }
                            if (!AppCircleCheckUtil.checkAuth(PayTypeActivity.this,userData)){
                                return;
                            }
                            ARouter.getInstance().build(RConfig.PAY_ADD_PAY_TYPE).withSerializable(Constans.PAY_TYPE, data).navigation();
                        })
                ;
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        ArrayList<PayTypeBean> payTypeBeans = new ArrayList<>();
        payTypeBeans.add(new PayTypeBean(PayTypeEnum.WX));
        payTypeBeans.add(new PayTypeBean(PayTypeEnum.ALIPAY));
        payTypeBeans.add(new PayTypeBean(PayTypeEnum.BANK));
        slimAdapter.updateData(payTypeBeans);
    }

    @Override
    protected void initData() {
        mPresenter.getData();
        userViewModel.getUsers().observe(this,users->{
            if (users!=null&&users.size()>0){
                userData = users.get(0);
            }else {
                 userData=null;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.getData();
        }
    }

    public void updateData(ArrayList<PayTypeBean> payTypeBeans) {
        slimAdapter.updateData(payTypeBeans);
    }

    public void toggleSuccess() {
        mPresenter.getData();
    }
}
